package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import lombok.Lombok;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result page(EmpQueryParam param) {
        log.info("员工列表查询条件：{}", param);
        // new 一个Page注意必须要baomidou包下面的
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());
        //按照创建时间进行倒序排序
        page.addOrder(OrderItem.desc("create_time"));
        //查询分页结构
        LambdaQueryWrapper<Emp> lqw = new LambdaQueryWrapper();
        //条件查询
        lqw.like(param.getName()!=null && param.getName().length() > 0,Emp::getName,param.getName())
                .eq(param.getGender() !=null,Emp::getGender,param.getGender())
                .between(param.getBegin() !=null && param.getEnd() != null,Emp::getCreateTime,param.getBegin(),param.getEnd());
        page = empService.page(page,lqw);
        //拿到分页的结果
        List<Emp> empList = page.getRecords();
        //拿到上面部门集合的部门id
        Set<Integer> collect = empList.stream().map(Emp::getDeptId).collect(Collectors.toSet());
        //根据部门id查询出对应的部门集合
        List<Dept> deptList = deptService.listByIds(collect);
        //查询dept表拿到部门id和部门名字对应的map
        Map<Integer, String> map = deptList.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
        //遍历员工集合  匹配其部门id对应的map表查出部门name
        for (Emp emp : empList) {
            emp.setDeptName(map.get(emp.getDeptId()));
        }
        //封装结果
        PageResult pageResult = new PageResult(page.getTotal(), empList);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增员工，员工信息：{}", emp);
        // 调用service层方法保存员工信息和员工的工作经历信息
        empService.saveEmp(emp);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("批量删除学生，{}", ids);
        // 调用service层方法批量删除员工信息和员工工作经历信息
        empService.removeEmpByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询员工信息，id：{}", id);
        Emp emp =   empService.getEmpById(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息，员工信息：{}", emp);
        empService.updateEmp(emp);
        // 调用service层方法修改员工信息
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        log.info("查询员工列表");
        empService.list();
        // 调用service层方法查询员工列表
        return Result.success();
    }
}
